package view.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import model.AppModel;
import model.WordModel;
import view.AppView;
import controller.*;

public class WordPanel extends JPanel {

	private static final long serialVersionUID = -4046757478456197850L;

	private WordModel wordModel;
	
	private WordSentencePanel visibleWordSentencePanel;
	private WordSentencePanel correctWordSentencePanel;
	
	private JLabel status;
	private JTextField input;
	private JButton next;
	
	private ArrayList<JButton> accentButtons;

	private InputWordListener inputWordListener;
	private NextListener nextListener;

	public WordPanel(AppView view, AppModel model)
	{
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		status = new JLabel();
		status.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(status, BorderLayout.NORTH);

		
		visibleWordSentencePanel = new WordSentencePanel();
		correctWordSentencePanel = new WordSentencePanel();

		JPanel wrapperCentre = new JPanel();
		wrapperCentre.add(visibleWordSentencePanel);
		wrapperCentre.add(correctWordSentencePanel);
		this.add(wrapperCentre, BorderLayout.CENTER);

		
		input = new JTextField();
		inputWordListener = new InputWordListener(input,view);
		input.addKeyListener(inputWordListener);
		input.setTransferHandler(null);
		input.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		input.setHorizontalAlignment(SwingConstants.CENTER);
		input.setColumns(10);
		
		next = new JButton("Next");
		nextListener = new NextListener(view);
		next.addActionListener(nextListener);
		next.addKeyListener(nextListener);
		
		accentButtons = new ArrayList<JButton>();
		JButton button = null;
		String[][] accents = model.getAccents();
		for(int i = 0; i < accents.length; i++)
		{
			final String keyboardLetter = accents[i][0];
			final String spanishLetter = accents[i][1];
			button = new JButton(spanishLetter);
			accentButtons.add(button);
			AccentButtonListener abl = new AccentButtonListener(spanishLetter, keyboardLetter, input);
			button.addActionListener(abl);
			input.addKeyListener(abl);
		}

		JPanel wrapperDown = new JPanel();
		wrapperDown.add(input);
		wrapperDown.add(next);
		for(JButton jb : this.accentButtons)
		{
			wrapperDown.add(jb);
		}
		this.add(wrapperDown, BorderLayout.SOUTH);
	}

	public void update (AppModel model)
	{
		this.wordModel = model.getCurrentWord();
		
		String visibleSentence = wordModel.isSentenceSet() ? wordModel.getSentenceDisplay() : "";
		visibleWordSentencePanel.update(wordModel.getDisplay(), visibleSentence);
		
		if(wordModel.isUsed())
		{
			wrongAnswer();
		}
		else
		{
			newWordLoaded();
		}
		status.setText(model.getTestMarks() + ", left: "+model.getWordsLeft());
		
		inputWordListener.update(model);
		nextListener.update(model);
	}

	private void wrongAnswer()
	{
		String correctSentence = wordModel.isSentenceSet() ? wordModel.getSentenceCorrect() : "";
		correctWordSentencePanel.update(wordModel.getCorrect(), correctSentence);

		input.setEditable(false);
		input.setEnabled(false);
		
		next.setEnabled(true);
		
		for(JButton jb : this.accentButtons)
		{
			jb.setEnabled(false);
		}
		next.requestFocus();
	}
	
	private void newWordLoaded()
	{
		correctWordSentencePanel.reset();
		input.setEnabled(true);
		input.setEditable(true);
		input.setText("");
		next.setEnabled(false);
		
		for(JButton jb : this.accentButtons)
		{
			jb.setEnabled(true);
		}
		input.requestFocus();
	}
}